package org.vectrics.common.view;

import org.apache.ecs.html.Div;
import org.apache.ecs.html.Input;
import org.vectrics.common.html.Combo;
import org.vectrics.common.html.TextHelper;

public class HtmlFormTimeEditRow extends HtmlFormRow {
	private boolean readOnly = false;
	private String hourParamName = null;
	private String minuteParamName = null;
	private String amPmParamName = null;

	public HtmlFormTimeEditRow(HtmlForm htmlForm, String hourParamName, String minuteParamName, 
			String amPmParamName, boolean required, boolean readOnly) {
		super(htmlForm, hourParamName + "_" + minuteParamName, required);
		this.hourParamName = hourParamName;
		this.minuteParamName = minuteParamName;
		this.amPmParamName  = amPmParamName;
		this.readOnly = readOnly;
		
		Div valueDiv = this.getValueDiv();
		
        Input editBox = makeEdit(this.hourParamName);
        editBox.setOnKeyPress(" return soloNumerosEnteros(event, this);");
        editBox.setClass("formDateFieldClass");
        String hour = this.getHtmlForm().getParameterValue(hourParamName);
        while (hour.length() < 2) {
        	hour = "0" + hour;
        }
        editBox.setValue(hour);
        valueDiv.addElement(editBox);

        Input minuteEditBox = makeEdit(this.minuteParamName);
        minuteEditBox.setOnKeyPress(" return soloNumerosEnteros(event, this);");
        minuteEditBox.setClass("formDateFieldClass");
        String minutes = this.getHtmlForm().getParameterValue(minuteParamName);
        while (minutes.length() < 2) {
        	minutes = "0" + minutes;
        }
        minuteEditBox.setValue(minutes);
        valueDiv.addElement(minuteEditBox);
        
        Combo ampmCombo = this.makeCombo(this.amPmParamName);
        ampmCombo.addOption("am", "am");
        ampmCombo.addOption("pm", "pm");
        ampmCombo.setStyle("width:60");
        String value = this.getHtmlForm().getParameterValue(amPmParamName);
        ampmCombo.selectOption(value);
        ampmCombo.setClass("formDateFieldClass");
        valueDiv.addElement(ampmCombo);
	}

	
	public void setValue(String hour, String minute) {
        if (readOnly) {
            this.getValueDiv().addElement(hour + ":" + minute); 
        } else {
        	Div valueDiv = this.getValueDiv();

        	Input editBox = makeEdit(hourParamName);
        	editBox.setOnKeyPress(" return soloNumerosEnteros(event, this);");
            editBox.setClass("formDateFieldClass");
            valueDiv.addElement(editBox);
            
            valueDiv.addElement("&nbsp;:&nbsp;");
            editBox = makeEdit(minuteParamName);
        	editBox.setOnKeyPress(" return soloNumerosEnteros(event, this);");
            String minutes = this.getHtmlForm().getParameterValue(minuteParamName);
            while (minutes.length() < 2) {
            	minutes = "0" + minutes;
            }
            editBox.setValue(minutes);
            editBox.setClass("formDateFieldClass");
            valueDiv.addElement(editBox);

            Input amRadio = new Input();
            amRadio.setType(Input.radio);
            amRadio.setName(this.amPmParamName);
            amRadio.setValue("am");
            amRadio.setClass("formAmPmRadioClass");
            amRadio.addElement("am");
            valueDiv.addElement(amRadio);

            Input pmRadio = new Input();
            pmRadio.setType(Input.radio);
            pmRadio.setName(this.amPmParamName);
            pmRadio.setValue("pm");
            pmRadio.addElement(TextHelper.getFont("subFont", "pm"));
            pmRadio.setClass("formAmPmRadioClass");
            valueDiv.addElement(pmRadio);
            
            String ampmValue = this.getHtmlForm().getParameterValue(amPmParamName);
            if (ampmValue.trim().toLowerCase().equals("pm")) {
            	amRadio.setChecked(false);
            	pmRadio.setChecked(true);
            } else {
            	amRadio.setChecked(true);
            	pmRadio.setChecked(false);
            }
            
        	String elementId = "am_" + this.amPmParamName;
        	amRadio.setID(elementId);
        	this.getHtmlForm().setFocusElementIfNull(elementId);
            
        }
		
	}
	
}
